package com.lanou.moving.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.lanou.moving.R;
import com.lanou.moving.entity.dao.Collection;
import com.lanou.moving.utils.Exchange;
import java.util.List;

/**
 * Created by 刘琪 on 15/11/6.
 */
public class CollectionAdapter extends BaseAdapter {
    private Context context;
    private List<Collection> collections;
    private Collection collection;

    public CollectionAdapter(Context context, List<Collection> collections) {
        this.context = context;
        this.collections = collections;
    }

    public void longClick(Collection collection){
        collections.remove(collection);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return collections != null && collections.size() > 0 ? collections.size() : 0;
    }

    @Override
    public Collection getItem(int position) {
        return collections.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.localmusic_item, null);
            holder.music_name = (TextView) convertView.findViewById(R.id.music_name);
            holder.music_singer = (TextView) convertView.findViewById(R.id.music_singer);
            holder.music_duration = (TextView) convertView.findViewById(R.id.music_duration);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        collection = getItem(position);

        if (collection != null) {
            // 歌名
            holder.music_name.setText(collection.getSongName());
            // 歌手名
            holder.music_singer.setText(collection.getSingerName());
            // 时长
            holder.music_duration.setText(String.valueOf(Exchange.formatTime(collection.getDuration())));
        }
        return convertView;
    }

    private class ViewHolder {
        public TextView music_name;
        public TextView music_singer;
        public TextView music_duration;
    }
}
